import { LoopFunc } from "../../Types";
import { Room } from "../../utils/Room";
import { ConnectClosestRooms } from "./ConnectClosestRooms";

/** 连接房间的函数，已经内置 */
export const linkRoom: LoopFunc = function (map, MapGenerator) {
    const rooms = MapGenerator.roomTiles.map((coords) => new Room(coords, map));
    MapGenerator.rooms = rooms;

    // ! 最耗时的就是 ConnectClosestRooms 这个阶段
    const links = ConnectClosestRooms(rooms);

    console.table({
        房间连接数: rooms.map((i) => i.connectedRooms.length),
        房间大小: rooms.map((i) => i.tiles.length),
    });

    //
    links.forEach(([from, to]) => {
        MapGenerator.ConnectRoom(from, to, map);
    });

    // 使用完成进行销毁
    MapGenerator.roomTiles = [];
    return map;
};
